//
//  MainViewController.swift
//  WeiBo test
//
//  Created by Aquarius_wing on 16/5/4.
//  Copyright © 2016年 Wing. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //希望它安全,就加上了private,但是会报错---找不到该函数,故而加上@objc
    @objc private func composeDidClick(){
        let compose = ComposeViewController()
        let nav = UINavigationController(rootViewController: compose)
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //由于tabBar是只读的,不能够直接操作,如果要修改,可以使用KVC
        let mainTabBar=MainTabBar()
        //KVC赋值
        setValue(mainTabBar, forKey: "tabBar")
        // 添加子视图控制器
        addChildViewControllers()
        mainTabBar.backgroundImage=UIImage(named: "tabbar_background")
        //classForCoder 打印函数类名
//        print(tabBar.classForCoder)
        
        //添加点击事件
        mainTabBar.composeBtn.addTarget(self, action: #selector(MainViewController.composeDidClick), forControlEvents: UIControlEvents.TouchUpInside)
    }

    private func addChildViewControllers() {
        //CVR模式
        addChildViewController(HomeTableViewController(), title: "奏折", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
       
    }

    //函数重载
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        //用颜色解决渲染效果
        self.tabBar.tintColor=themeColor
        //新建一个导航控制器
        let nav=UINavigationController(rootViewController: vc)
        //title这个是由内向外设置的,TabBarController包含NavigationController,包含HomeTableViewController
        vc.title=title
        vc.tabBarItem.image=UIImage(named: imageName)
        vc.tabBarItem.selectedImage=UIImage(named: imageName+"_highlighted")
        addChildViewController(nav)
    }
    
}
